/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.StorageBin;
import org.crystalrange.enterprise.service.StorageBinService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class StorageBinListController extends BaseController {

    protected StorageBinService storageBinService;
    protected StorageBin selectedStorageBin;
    protected Listbox listboxStorageBin;
    protected Search searchStorageBin;
    protected List<StorageBin> storageBins;

    public StorageBinService getStorageBinService() {
        return this.storageBinService;
    }

    public void setStorageBinService(StorageBinService storageBinService) {
        this.storageBinService = storageBinService;
    }

    public StorageBin getSelectedStorageBin() {
        return this.selectedStorageBin;
    }

    public void setSelectedStorageBin(StorageBin selectedStorageBin) {
        this.selectedStorageBin = selectedStorageBin;
    }

    public List<StorageBin> getStorageBins() {
        return this.storageBins;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchStorageBin = new Search(StorageBin.class);
        this.searchStorageBin.setMaxResults(100);
        this.searchStorageBin.addFetch("warehouseNumber");
        this.searchStorageBin.addFetch("storageType");
        this.searchStorageBin.addFetch("storageSection");
        this.searchStorageBin.addFetch("createdBy");
        this.searchStorageBin.addFetch("updatedBy");
        this.storageBins = this.storageBinService.search(this.searchStorageBin);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemStorageBinCreate(Event event) throws Exception {
        this.storageBinCreate();
    }

    public void onClick$menuitemStorageBinEdit(Event event) throws Exception {
        this.storageBinEdit();
    }

    public void onClick$menuitemStorageBinDelete(Event event) throws Exception {
        this.storageBinDelete();
    }

    public void onClick$menuitemStorageBinSearch(Event event) throws Exception {
        this.storageBinSearch();
    }

    public void onClick$menuitemStorageBinRefresh(Event event) throws Exception {
        this.storageBinRefresh();
    }

    public void onClick$toolbarbuttonStorageBinCreate(Event event) throws Exception {
        this.storageBinCreate();
    }

    public void onClick$toolbarbuttonStorageBinEdit(Event event) throws Exception {
        this.storageBinEdit();
    }

    public void onClick$toolbarbuttonStorageBinDelete(Event event) throws Exception {
        this.storageBinDelete();
    }

    public void onClick$toolbarbuttonStorageBinSearch(Event event) throws Exception {
        this.storageBinSearch();
    }

    public void onClick$toolbarbuttonStorageBinRefresh(Event event) throws Exception {
        this.storageBinRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.storageBinEdit();
    }
    
    private void storageBinCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/storageBin.zul");
    }

    private void storageBinEdit() throws Exception {
        if (this.selectedStorageBin != null) {
            this.execution.sendRedirect("~./zul/html/storageBin.zul?Id=" + this.selectedStorageBin.getId().toString());
        }
    }

    private void storageBinDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxStorageBin.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    StorageBin storageBin = (StorageBin) selectedItem.getValue();
                    this.storageBinService.remove(storageBin);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.storageBins = this.storageBinService.search(this.searchStorageBin);
        this.binder.loadAll();
    }

    private void storageBinSearch() throws Exception {
        this.execution.setAttribute("searchStorageBin", this.searchStorageBin);

        Window window = (Window) this.execution.createComponents("~./zul/html/storageBinSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchStorageBin = (Search) this.execution.getAttribute("searchStorageBin");
        }

        this.storageBins = this.storageBinService.search(this.searchStorageBin);
        this.binder.loadAll();
    }

    private void storageBinRefresh() throws Exception {
        this.storageBins = this.storageBinService.search(this.searchStorageBin);
        this.binder.loadAll();
    }
}
